package com.skyeyemonitoring.activitys;

import android.app.ActionBar;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.skyeyemonitoring.R;
import com.skyeyemonitoring.adapter.AdapterQueryDate;
import com.skyeyemonitoring.base.BaseActivity;
import com.skyeyemonitoring.entity.EntityQueryDete;
import com.skyeyemonitoring.entity.ScreenInfo;
import com.skyeyemonitoring.entity.WheelMain;
import com.skyeyemonitoring.utils.DateUtils;
import com.skyeyemonitoring.utils.JudgeDate;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日志查询
 * Created by ${QQZ} on 2017/5/22.
 */

public class LocalQueryDate extends BaseActivity implements View.OnClickListener {


    private WheelMain wheelMainDate;
    private Button mTv_center;
    /** 2017-5-22 22:39 */
    private TextView mLocalQueryDateStart;
    /** 2017-5-22 22:39 */
    private TextView mLocalQueryDateEnd;
    /** 数据列表 */
    private RecyclerView mrlv_local_query_list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qqz_local_query_date);
        setBackBtn("日志查询");
        initView();


        List<EntityQueryDete> list = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            EntityQueryDete entityQueryDete = new EntityQueryDete();
            entityQueryDete.setGatherTime("2019391"+i);
            entityQueryDete.setIncline(i+"423");
            entityQueryDete.setShake(i+"4234"+i);

            list.add(entityQueryDete);
    }




        mrlv_local_query_list.setLayoutManager(new LinearLayoutManager(this));
        AdapterQueryDate queryDate = new AdapterQueryDate(list);
        mrlv_local_query_list.setAdapter(queryDate);

    }


    /**
     * 时间选择器popowindo
     *
     * @param timeType
     */
    public void showBottoPopupWindow(final String timeType) {
        WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = manager.getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        defaultDisplay.getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        View menuView = LayoutInflater.from(this).inflate(R.layout.show_popup_window, null);
        final PopupWindow mPopupWindow = new PopupWindow(menuView, (int) (width * 0.8), ActionBar.LayoutParams.WRAP_CONTENT);

        ScreenInfo screenInfoDate = new ScreenInfo(this);
        wheelMainDate = new WheelMain(menuView, true);
        wheelMainDate.screenheight = screenInfoDate.getHeight();
        String time = DateUtils.currentMonth().toString();
        Calendar calendar = Calendar.getInstance();
        if (JudgeDate.isDate(time, "yyyy-MM-DD")) {
            try {
                calendar.setTime(new Date(time));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        wheelMainDate.initDateTimePicker(year, month, day, hours, minute);
        mPopupWindow.setAnimationStyle(R.style.AnimationPreview);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAtLocation(mTv_center, Gravity.CENTER, 0, 0);
        mPopupWindow.setOnDismissListener(new poponDismissListener());
        //半透明色
//        backgroundAlpha(0.6f);
        TextView tv_cancle = (TextView) menuView.findViewById(R.id.tv_cancle);
        TextView tv_ensure = (TextView) menuView.findViewById(R.id.tv_ensure);
        TextView tv_pop_title = (TextView) menuView.findViewById(R.id.tv_pop_title);

        if (timeType.equals("start")) {
            tv_pop_title.setText("选择起始时间");
        } else {
            tv_pop_title.setText("选择结束时间");
        }
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mPopupWindow.dismiss();
                backgroundAlpha(1f);
            }
        });
        tv_ensure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String beginTime = wheelMainDate.getTime().toString();

                if (timeType.equals("start")) {
                    mLocalQueryDateStart.setText(DateUtils.formateStringH(beginTime, DateUtils.yyyyMMddHHmm));
                } else {
                    mLocalQueryDateEnd.setText(DateUtils.formateStringH(beginTime, DateUtils.yyyyMMddHHmm));
                }

                mPopupWindow.dismiss();
                backgroundAlpha(1f);
            }
        });
    }


    /**
     * 初始化控件
     */
    public void initView() {
        mTv_center = (Button) findViewById(R.id.tv_center);
        mLocalQueryDateStart = (TextView) findViewById(R.id.local_query_date_start);
        mLocalQueryDateStart.setOnClickListener(this);
        mLocalQueryDateEnd = (TextView) findViewById(R.id.local_query_date_end);
        mLocalQueryDateEnd.setOnClickListener(this);
        mrlv_local_query_list = (RecyclerView) findViewById(R.id.rlv_local_query_list);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.local_query_date_start:
                showBottoPopupWindow("start");
                break;
            case R.id.local_query_date_end:
                showBottoPopupWindow("end");
                break;
        }
    }



    class poponDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }

    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }
}
